<template>
  <div class="customers-head-container">
    <!-- 搜索选项 -->
    <div class="customers-head-top">
      <el-row style="height:32px;line-height:32px">
        <el-col :span="10">
          <div class="customers-head-top-col" style="padding-left:0">
            <span style="padding:0 5px">时间范围</span>
            <el-date-picker style="width:100%"
              v-model="time"
              type="datetimerange"
              :picker-options="pickerOptions"
              range-separator="至"
              start-placeholde="开始时间"
              end-placeholde="结束时间"
              size="small">
            </el-date-picker>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="customers-head-top-col">
            <span style="padding:0 5px">关键词</span>
            <el-input v-model="tuanParams.searchName" placeholder="请输入昵称、真实姓名或手机号" size="small"></el-input>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="customers-head-top-col">
            <span style="padding:0 5px">城市</span>
            <el-input v-model="tuanParams.city" placeholder="请输入城市" size="small"></el-input>
          </div>
        </el-col>
        
        <el-col :span="2">
          <el-button type="primary" size="small" @click="search">搜索</el-button>
        </el-col>
      </el-row>
    </div>

    <!-- 表格 -->
    <div>
      <el-table :data="TuanList" style="width: 100%" stripe>
        <el-table-column label="头像" align="center">
          <template slot-scope="scope">
            <l-pic :picurl="scope.row.avatar" :size="{width:40,height:40}" :disdel="false" :disview="false"/>
          </template>
        </el-table-column>
        <el-table-column prop="nickname" label="昵称" align="center"></el-table-column>
        <el-table-column label="团长信息" align="center">
          <template slot-scope="scope">
            <div>
              <div>{{ scope.row.realname }}</div>
              <div>{{ scope.row.phone }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="所属城市" align="center">
          <template slot-scope="scope">
            <div>
              <div>{{ scope.row.province }}</div>
              <div>{{ scope.row.city }}</div>
              <div>{{ scope.row.area }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="自提点信息">
          <template slot-scope="scope">
            <div>
              <div><span>小区：</span>{{ scope.row.area_name }}</div>
              <div><span>自提点：</span>{{ scope.row.addr }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="is_tuan_express" label="团长配送费" align="center"></el-table-column>
        <el-table-column prop="number" label="总订单数" align="center"></el-table-column>
        <el-table-column label="旗下团员" align="center">
          <template slot-scope="scope">
            <div>
              <el-button type="text" style="display:inline-block" @click="writeOff(scope.row)">{{ scope.row.count }}</el-button>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="佣金">
          <template slot-scope="scope">
            <div>
              <div><span>总消费佣金：</span>{{ scope.row.sum_money === null ? 0.00 : scope.row.sum_money }}</div>
              <div><span>待结算佣金：</span>{{ scope.row.on_money === null ? 0.00 : scope.row.on_money }}</div>
              <div><span>待提现佣金：</span>{{ scope.row.user_balance === null ? 0.00 : scope.row.user_balance }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <div>
              <el-tag effect="dark" v-if="scope.row.state === '0'" size="small" type="success">正常</el-tag>
              <el-tag effect="dark" v-if="scope.row.state === '1'" size="small" type="danger">冻结</el-tag>
              <el-tag effect="dark" v-if="scope.row.state === '2'" size="small" type="info">关闭</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="200">
          <template slot-scope="scope">
            <div>
              <el-button type="text" style="display:inline-block" @click="addGoods(scope.row)">绑定商品</el-button>
              <!--<el-button type="text" style="display:inline-block" @click="delTuanUser(scope.row)">解绑会员</el-button>-->
              <el-button type="text" style="display:inline-block" @click="edit(scope.row)">编辑</el-button>
              <el-button type="text" style="display:inline-block" @click="addWriteOff(scope.row)">添加核销员</el-button>
              <el-button type="text" style="display:inline-block" @click="showWriteOff(scope.row)">查看核销员</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div style="text-align:right;margin:15px 15px">
      <el-pagination class="page"
        background
        layout="total, prev, pager, next, jumper"
        :total="count"
        :page-size="10"
        :current-page="page"
        @size-change="changePage"
        @current-change="changePage">
      </el-pagination>
    </div>


    <!-- 查看旗下团员 -->
    <el-dialog :visible.sync="disWriteOff" width="40%" title="旗下团员">
      <write-off :tuanId="tuanId" @success="disWriteOff = false;" v-if="disWriteOff"/>
    </el-dialog>

    <!-- 绑定商品 -->
    <el-dialog :visible.sync="disAddGoods" width="40%" title="绑定商品">
      <add-goods :tuanId="tuanId" @success="disAddGoods = false; getTuanList()" v-if="disAddGoods"/>
    </el-dialog>

    <!-- 添加核销员 -->
    <el-dialog :visible.sync="disAddWriteOff" width="40%" title="绑定核销员">
      <add-writeOff :tuanId="tuanId" @success="disAddWriteOff = false;" v-if="disAddWriteOff"/>
    </el-dialog>

    <!-- 查看核销员 -->
    <el-dialog :visible.sync="disShowWriteOff" width="40%" title="查看核销员">
      <show-writeOff :tuanId="tuanId" @success="disShowWriteOff = false;" v-if="disShowWriteOff"/>
    </el-dialog>

    <!-- 编辑 -->
    <el-dialog :visible.sync="disEdit" width="40%" title="编辑" top="0">
      <edit :tuanId="tuanId" v-if="disEdit"  @success="disEdit = false; getTuanList()"/>
    </el-dialog>
  </div>
</template>

<script>
import { getTuanUserList, delTuanUser } from '@/api/customers'
import LPic from '@/components/lPic'
import { parseTime } from '@/utils/index'
import writeOff from './components/writeOff'
import addGoods from './components/addGoods'
import addWriteOff from './components/addWriteOff'
import showWriteOff from './components/showWriteOff'
import edit from './components/edit'

export default {
  name: 'customershead',
  components: {
    LPic,
    addGoods,
    writeOff,
    addWriteOff,
    showWriteOff,
    edit
  },
  data() {
    return {
      page: 1,
      count: 1,

      tuanId: '',

      tuanParams: {
        key: this.$store.state.app.activeApp.saa_key,
        type: 1,
        is_self: 1,
        audit_time: '',
        searchName: '',
        city: '',
        limit: 10,
        page: 1
      },
      time: '',
      TuanList: [],

      disWriteOff: false,
      disAddGoods: false,
      disAddWriteOff: false,
      disShowWriteOff: false,
      disEdit: false,
      pickerOptions: {
        shortcuts: [{
          text: '近7天',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start,end]);
          }
        },
        {
          text: '近30天',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start,end]);
          }
        }]
      }
    }
  },
  mounted() {
    this.getTuanList();
  },
  watch: {
    time(newTime) {
      if(newTime!==null){
        const startTime = parseTime(newTime[0],'{y}-{m}-{d}');
        const endTime = parseTime(newTime[1],'{y}-{m}-{d}');  
        this.tuanParams.audit_time = startTime + ' to ' + endTime;
      }else{
        this.tuanParams.audit_time = '';
      }        
    }
  },
  methods: {
    /**
     * 获取团长列表
     */
    getTuanList() {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        type: 1,
        is_self: 1,
        limit: 10,
        page: this.page
      }
      getTuanUserList(params).then(res => {
        if(res.status === 200){
          this.TuanList = res.data;
          this.count = res.count;
        }else if(res.status === 204){
          this.TuanList = [];
          this.count = 1;
        }else{
          this.$message.error(res.message);
        }
      })
    },
    /**
     * 翻页
     */
    changePage(page) {
      this.page = page;
      this.getTuanList();
    },
    /**
     * 搜索
     */
    search() {
      // audit_time: '',
      //         searchName: '',
      //         city: '',
      if (this.tuanParams.audit_time === '') {
        delete this.tuanParams.audit_time;
      }
      if (this.tuanParams.searchName === '') {
        delete this.tuanParams.searchName;
      }
      if (this.tuanParams.city === '') {
        delete this.tuanParams.city;
      }
      getTuanUserList(this.tuanParams).then(res => {
        if(res.status === 200){
          this.TuanList = res.data;
          this.count = res.count;
        }else if(res.status === 204){
          this.TuanList = [];
          this.count = 1;
        }else{
          this.$message.error(res.message);
        }
      })
    },
    /**
     * 查看旗下团员
     */
    writeOff(data) {
      this.tuanId = data.uid;
      this.disWriteOff = true;
    },
    /**
     * 添加商品
     */
    addGoods(data) {
      this.tuanId = data.id;
      this.disAddGoods = true;
    },
    /**
     * 添加核销员
     */
    addWriteOff(data) {
      this.tuanId = data.uid;
      this.disAddWriteOff = true;
    },
    /**
     * 查看核销员
     */
    showWriteOff(data) {
      this.tuanId = data.uid;
      this.disShowWriteOff = true;
    },
    /**
     * 解绑会员
     */
    delTuanUser(data) {
      const params = { id: data.id, key: this.$store.state.app.activeApp.saa_key };
      delTuanUser(params).then(res => {
        if(res.status === 200){
          this.$message.success('解绑成功！');
          this.getTuanList();
        }else{
          this.$message.error(res.message);
        }
      })
    },
    /**
     * 编辑
     */
    edit(data) {
      this.tuanId = data.id
      this.disEdit = true;
    }
  },
}
</script>

<style lang="scss" scoped>
.customers-head-container{
  min-height: calc(100vh - 100px);
  margin: 20px;
  background-color: #fff;
  padding:25px;
  box-shadow: 0px 0px 4px 0px rgba(115, 115, 115, 0.2);
}
.customers-head-top{
  margin: 20px auto;
  text-align: center;
  height: 40px;
  line-height: 40px;
  &-col{
    padding: 0 15px;
    max-width: 98%;
    overflow: hidden;
    display: flex;
    &>span{
      flex: 2;
      display: inline-block;
      padding: 0 5px;
      white-space: nowrap; 
      overflow: hidden;
    }
    &>div{
      flex: 4
    }
  }
}
</style>